package com.net.xpay.web.controller.merchant.safePay;


import com.google.common.base.Preconditions;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.model.ResultPagedList;
import com.net.xpay.common.domain.SafePayExcelTemplate;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.SafePayExcelTemplateManager;
import com.net.xpay.common.model.CreateSafePayExcelTemplateModel;
import com.net.xpay.common.model.ModifySafePayExcelTemplateModel;
import com.net.xpay.common.service.SafePayExcelTemplateService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.SafePayExcelTemplateSearchModel;
import com.net.xpay.core.model.IdModel;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/merchant/safe-pay-excel-template")
@Api(tags = "商户/安全付自定义表格", description = "商户 安全付自定义表格 net")
public class SafePayExcelTemplateController {

    @Autowired
    private SafePayExcelTemplateManager safePayExcelTemplateManager;

    @Autowired
    private SafePayExcelTemplateService safePayExcelTemplateService;


    /**
     * 创建模板
     */
    @ApiOperation("创建模板")
    @PostMapping
    public ResultData<Void> addSafePayExcelTemplate(@ApiIgnore @RequestUser User merchant, @RequestBody CreateSafePayExcelTemplateModel model) {
        safePayExcelTemplateService.addSafePayExcelTemplate(merchant, model);
        return new ResultData<>();
    }

    /**
     * 修改模板
     */
    @ApiOperation("修改模板")
    @PostMapping("/modify")
    public ResultData<Void> modifySafePayExcelTemplate(@ApiIgnore @RequestUser User merchant, @RequestBody ModifySafePayExcelTemplateModel model) {
        safePayExcelTemplateService.modifySafePayExcelTemplate(merchant, model);
        return new ResultData<>();
    }


    /**
     * 删除模板
     */
    @ApiOperation("删除模板")
    @PostMapping("/delete")
    public ResultData<Void> deleteSafePayExcelTemplate(@ApiIgnore @RequestUser User merchant, @RequestBody IdModel idModel) {
        SafePayExcelTemplate template = safePayExcelTemplateManager.findById(merchant.getId(), idModel.getId());
        if (template == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "模板不存在");
        }
        safePayExcelTemplateManager.deleteById(idModel.getId());
        return new ResultData<>();
    }

    /**
     * 搜索
     */
    @ApiOperation("搜索")
    @PostMapping("/search")
    public ResultPagedList<SafePayExcelTemplate> search(@ApiIgnore @RequestUser User merchant, @RequestBody SafePayExcelTemplateSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        searchModel.setUserId(merchant.getId());
        List<SafePayExcelTemplate> records = safePayExcelTemplateManager.search(searchModel);
        long count = safePayExcelTemplateManager.searchCount(searchModel);

        return new ResultPagedList<>(records, count, searchModel);
    }


    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public ResultList<SafePayExcelTemplate> list(@ApiIgnore @RequestUser User merchant, @RequestBody SafePayExcelTemplateSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        searchModel.setUserId(merchant.getId());
        List<SafePayExcelTemplate> records = safePayExcelTemplateManager.list(searchModel);

        return new ResultList<>(records);
    }


}
